package org.jgs1904.apachepoidemo.controller;

import com.aliyun.oss.ServiceException;
import org.jgs1904.apachepoidemo.utils.ExcelExportExample;
import org.jgs1904.apachepoidemo.utils.ExcelImageReader;
import org.jgs1904.apachepoidemo.utils.ExcelImportExample;
import org.jgs1904.apachepoidemo.vo.OrderImportVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * @author: 默苍璃
 * @date: 2023-10-09 18:14
 */
@RestController
public class ExcelController {

    @Autowired
    private ExcelExportExample excelExportExample;

    @Autowired
    private ExcelImportExample excelImportExample;


    @GetMapping("export")
    public void exportTemplate(HttpServletResponse response) throws IOException {
        excelExportExample.excelExportExample(response);
    }

    @GetMapping("import")
    public void importTemplate(HttpServletRequest request, HttpServletResponse response) throws IOException {
        excelImportExample.excelImportExample(request);
    }


    /**
     * 导入Controller方法
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public String importExcelWapper(@RequestParam("file") MultipartFile file) throws IOException {
        //读取上传带图片文件转成Map
        Map<String, Map<Integer, Map<Integer, String>>> sheetsMap = ExcelImageReader.readExcelImageToMap(file);

        Set<String> keys = sheetsMap.keySet();
        List<String> sheetsNames = new ArrayList<>();
        // 获取所有的key == 页名称
        for (String sheetsName : keys) {
            sheetsNames.add(sheetsName);
        }
        // 遍历所有的列
        for (int i = 0; i < sheetsMap.size(); i++) {
            // 根据页名称获取页
            String sheetsName = sheetsNames.get(i);
            Map<Integer, Map<Integer, String>> rows = sheetsMap.get(sheetsName);
            if (rows.size() > 0) {
                // 根据所有的行遍历
                for (int rowIdx = 1; rowIdx < rows.size(); rowIdx++) {
                    // 遍历当前行的所有列
                    Map<Integer, String> columnsTitel = rows.get(0); // 第一行的标题
                    Map<Integer, String> columns = rows.get(rowIdx);
                    OrderImportVO orderImportVO = new OrderImportVO();
                    String offProperty = "";
                    String offProductId = "";
                    for (int colIdx = 0; colIdx < columnsTitel.size(); colIdx++) {
                        System.out.println(columns.get(colIdx));
                        String colValue = columns.get(colIdx);
                        if ("图片".equals(columnsTitel.get(colIdx))) {
                            String imageUrl = StringUtils.isEmpty(columns.get(colIdx)) ? "" : colValue;
                            orderImportVO.setImage(imageUrl);
                            if (StringUtils.isEmpty(imageUrl)) {
                                throw new ServiceException("图片不能为空！");
                            }
                        }
                    }
                }
            }
        }
        return "success！！！";
    }


}
